import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Quest implements Runnable {
    private Proxy proxy;
    private String path;
    private String link;
    private String tail;

    public Quest(Proxy proxy, String link, String path) {
        this.proxy = proxy;
        this.path = path;
        this.link = link;
        this.tail = ".jpg";
    }

    @Override
    public void run() {
        while (true) {
            try {
                String target = link + tail;
                System.out.println("-->> Try " + target);
                URL url = new URL(target);
                URLConnection connection = proxy == null ? url.openConnection() : url.openConnection(proxy);
                connection.setRequestProperty("accept", "*/*");
                connection.setRequestProperty("connection", "Keep-Alive");
                connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36");
                Map<String, List<String>> map = connection.getHeaderFields();
                String resp = map.get(null).get(0);
                if (resp.equals("HTTP/1.1 404 Not Found")) {
                    doChange();
                    continue;
                }
                if (!resp.equals("HTTP/1.1 200 OK")) {
                    continue;
                }
                if (resp.equals("HTTP/1.1 200 OK")) {
                    System.out.println("-->> Download " + connection.getURL());
                    String fileName = target.substring(target.lastIndexOf("/"));
                    File file = new File(path + fileName);
                    Long fileSize = file.length();
                    Integer urlSize = connection.getContentLength();
                    while (!file.exists() || fileSize < urlSize) {
                        int byteSum = 0;
                        int byteCur = 0;
                        InputStream in = connection.getInputStream();
                        FileOutputStream fs = new FileOutputStream(file);
                        byte[] buffer = new byte[1024];
                        while ((byteCur = in.read(buffer)) != -1) {
                            byteSum += byteCur;
                            fs.write(buffer, 0, byteCur);
                        }
                        in.close();
                        fs.flush();
                        fs.close();
                        fileSize = file.length();
                    }
                    System.out.println("-->> Success " + connection.getURL());
                    break;
                }
            } catch (Exception e) {
                continue;
            }
        }
    }

    private void doChange() {
        switch (tail) {
            case ".jpg":
                tail = ".png";
                break;
            case ".png":
                tail = ".gif";
                break;
            case ".gif":
                tail = ".jpeg";
                break;
            case ".jpeg":
                tail = ".webp";
                break;
            case ".webp":
                tail = ".jpg";
                break;
        }
    }
}
